package compile;

/**
 * @author SongJun
 * @description TODO:
 * @date 2021/9/28 17:02
 */
public class Intepretor extends AstVisitor {
    Object visitProg(Prog prog){
        Object retVal = null;
        for(Statement x : prog.getStmts()){
            if(x instanceof FunctionCall){
                retVal = this.runFunction((FunctionCall)x);
            }
        }
        return retVal;
    }
    Object visitFunctionBody(FunctionBody functionBody){
        Object retVal = null;
        for(Statement x : functionBody.stmts){
            retVal = this.runFunction((FunctionCall)x);
        }
        return null;
    }
    private Object runFunction(FunctionCall functionCall){
        if (functionCall.name.equals("println")){ //内置函数
            if(functionCall.parameters.size()>0){
                System.out.println(functionCall.parameters.get(0));
            } else {

            }
            return 0;
        } else { //找到函数定义，继续遍历函数体
            if (functionCall.definition !=null){
                this.visitFunctionBody(functionCall.definition.getBody());
            }
        }
        return null;
    }
}
